<?php
include_once 'protocol_includes.php';

class RequestChat extends subapplication
{
    private $cmd;
    public static $PROTOCOL = "RequestChat";

    public static function create(command $cmd)
    {
        if (!self::IsThisClass($cmd)) return null;
        $N = new RequestChat($cmd);
        return $N;
    }

    public function __construct(command $cmd)
    {
        $this->cmd  = $cmd;
        $this->LoginKey($cmd->GetData('LOGINKEY'));
    }

    public function  requiresAuthentication() {
        return true;
    }
    
    public function execute()
    {
        $returnString = 'NS_PROTOCOL=ChatMessages&';        
        $returnString = $returnString . 'NS_MESSAGES=';
        
        $username = ns_dao_user::GetUsernameFromLoginKey($this->LoginKey());
        $softwareList = ns_dao_chat::GetNewMessages($username);

        if (is_array($softwareList))
            foreach ($softwareList as $rowKey => $rowData) {
                $returnString = $returnString . "`Sender=" . $rowData->Sender() . "|TimeSent=" . $rowData->TimeSent() . "|Message=" . $rowData->Message();
            }
            
        $this->UpdateResult($returnString);
    }


    // =========== PROPERTIES ==========

    private $property = array();
    private function GetSetData($property_name,$value = null)
    {
        if (is_null($value) && isset($this->property[$property_name])) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }

    public function LoginKey($value = null)
    {
        return $this->GetSetData("LOGINKEY",$value);
    }

    public function result()
    {
        $this->cmd->GetResult();
    }

    public function UpdateResult($str)
    {
        $this->cmd->SetResult($str);
    }

    public static function IsThisClass(command $cmd)
    {
        if (!$cmd->HasDataKey('LOGINKEY')) return false;
        return true;
    }
}

?>
